/*
 *  ECE552H1 F Assignment 2
 *  File Name: mb.c
 *  Author: Mustafa Abbas
 *  Description:
 *      A microbenchmark that executes a fixed number of
 *      conditional branches to test out branch predictors.
 */

#define BENCHMARK_SIZE 100000

int main () {
    int i = 0;
    int x = 0;
    int a = 0;
    int b = 0;

    /* The loop below has 4 branches, one for the loop and 3 for
       the if statements we can predict the outcome of each branch
       and use it to test the branch predictors:
       1) The loop branch will be taken exactly BENCHMARK_SIZE times
       2) The first if statement will be taken exactly 1/7 times
       3) The second if statement will be taken exactly 1/13 times
       4) The third if statement is correlated to the first two and
          will be taken only when the first two are taken.
     */
    for (i=0; i<BENCHMARK_SIZE; i++) {
        a = 0;
        b = 0;
        if (x%7 == 0)
            a=1;
        if (x%13 == 0)
            b=1;
        if (a == b)
            x++;
    }
    return 0;
}

/* 
   Here is the assembly output of the compiled program, the branches are marked
   with ==>

	.file	1 "mb.c"

 # GNU C 2.7.2.3 [AL 1.1, MM 40, tma 0.1] SimpleScalar running sstrix compiled by GNU C

 # Cc1 defaults:
 # -mgas -mgpOPT

 # Cc1 arguments (-G value = 8, Cpu = default, ISA = 1):
 # -quiet -dumpbase -O2 -o

gcc2_compiled.:
__gnu_compiled_c:
	.text
	.align	2
	.globl	main

	.text

	.loc	1 12
	.ent	main
main:
	.frame	$sp,24,$31		# vars= 0, regs= 1/0, args= 16, extra= 0
	.mask	0x80000000,-8
	.fmask	0x00000000,0
	subu	$sp,$sp,24
	sw	$31,16($sp)
	jal	__main
	move	$5,$0
	move	$9,$0
	li	$12,-1840709632			# 0x92490000
	ori	$12,$12,0x2493
	li	$11,0x4ec40000		# 1321467904
	ori	$11,$11,0xec4f
	li	$10,0x00010000		# 65536
	ori	$10,$10,0x869f
$L5:
	mult	$5,$12
	.set	noreorder
	mfhi	$3
	mflo	$2
	#nop
	.set	reorder
	move	$8,$0
	move	$7,$0
	sra	$6,$5,31
	srl	$2,$3,0
	move	$3,$0
	addu	$2,$5,$2
	sra	$2,$2,2
	subu	$2,$2,$6
	sll	$4,$2,3
	subu	$4,$4,$2
==>	bne	$5,$4,$L6
	li	$8,0x00000001		# 1
$L6:
	mult	$5,$11
	.set	noreorder
	mfhi	$3
	mflo	$2
	#nop
	#nop
	.set	reorder
	srl	$2,$3,0
	move	$3,$0
	sra	$2,$2,2
	subu	$2,$2,$6
	sll	$4,$2,1
	addu	$4,$4,$2
	sll	$4,$4,2
	addu	$4,$4,$2
==>	bne	$5,$4,$L7
	li	$7,0x00000001		# 1
$L7:
==>	bne	$8,$7,$L4
	addu	$5,$5,1
$L4:
	addu	$9,$9,1
	slt	$2,$10,$9
==>	beq	$2,$0,$L5
	move	$2,$0
	lw	$31,16($sp)
	addu	$sp,$sp,24
	j	$31
	.end	main
    */

